<template>
  <div id="login">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt>
      </div>
      <!-- 登录表单 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    reset() {
      // console.log(this)
      // console.log(this.$refs.loginFormRef)
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async flag => {
        // console.log(flag)
        if (flag) {
          // console.log('登录成功，可以交给后台处理')//发请求
          // console.log(this.$http)//获取axios
          const { data: res } = await this.$http.post('login', this.loginForm) //解构赋值
          console.log(res) //得到的是promise对象  用async await
          if (res.meta.status == 200) {
            this.$message({
              message: res.meta.msg,
              type: 'success'
            })
            sessionStorage.setItem('token', res.data.token) //
            this.$router.push('/home')
            // console.log('登录成功')
          } else {
            this.$message({
              message: res.meta.msg,
              type: 'warning'
            })
            // console.log(res.meta.msg)
          }
        }
      })
    }
  },

  created() {}
}
</script>

<style lang='css' scoped>
#login {
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar {
  width: 130px;
  height: 130px;
  overflow: hidden;
  border-radius: 50%;
  border: 1px solid #eee;
  padding: 10px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
}
.avatar img {
  width: 100%;
  height: 100%;
  background-color: #ccc;
  border-radius: 50%;
}
.el-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  float: right;
}
</style>
